<template>
  <div>
    <el-form ref="form" :rules="rules" style="padding-top: 10px;" :model="form" label-width="120px">
      <div class="com-form-title">基础信息</div>
      <el-row>
        <el-col :span="8">
          <el-form-item label="分包合同编号" prop="contractNumber">
            <el-input v-model="form.contractNumber" readonly :disabled="disableForm || optType !== 'add'" @click.native="optType === 'add' ? showTransferListModal = true : null">
              <i slot="suffix" class="el-input__icon el-icon-circle-plus-outline" />
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="分包合同名称" prop="contractName">
            <el-input v-model="form.contractName" :disabled="true" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="分包结算编号" prop="settleNumber">
            <el-input v-model="form.settleNumber" :disabled="true" placeholder="自动生成" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="分包队伍编号" prop="teamNumber">
            <el-input v-model="form.teamNumber" :disabled="true" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="分包队伍名称" prop="teamName">
            <el-input v-model="form.teamName" :disabled="true" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="合同额" prop="contractAmount">
            <el-input v-model="form.contractAmount" :disabled="true" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="结算期次(年+月)" prop="period">
            <el-input v-model="form.period" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="本次结算金额" prop="thisSettleAmount">
            <el-input v-model="form.thisSettleAmount" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="开累结算金额" prop="accruedSettleAmount">
            <el-input v-model="form.accruedSettleAmount" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="项目名称" prop="projectName">
            <el-input v-model="form.projectName" :disabled="true" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" :disabled="disableForm" />
          </el-form-item>
        </el-col>
      </el-row>

      <div class="btns" style="padding-bottom: 10px;">
        <el-button type="primary" :disabled="disableForm" @click="showAddDetailModal = true;">新增行</el-button>
        <el-button type="primary" :disabled="disableForm" @click="delDetailDataItem">删除行</el-button>
      </div>
      <com-table
        v-model="selectRow"
        :no-page="true"
        :columns="columns"
        highlight-current-row
        :data="detailDataList"
      >
        <template v-slot:conNumber="{row}">
          <el-input v-model="row.conNumber" :disabled="disableForm" />
        </template>
        <template v-slot:conName="{row}">
          <el-input v-model="row.conName" :disabled="disableForm" />
        </template>
        <template v-slot:conUnit="{row}">
          <el-input v-model="row.conUnit" :disabled="disableForm" />
        </template>
        <template v-slot:conUnitPrice="{row}">
          <el-input v-model="row.conUnitPrice" :disabled="disableForm" />
        </template>
        <template v-slot:conSumPrice="{row}">
          <el-input v-model="row.conSumPrice" :disabled="disableForm" />
        </template>
        <template v-slot:conCount="{row}">
          <el-input v-model="row.conCount" :disabled="disableForm" />
        </template>
        <template v-slot:tsCount="{row}">
          <el-input v-model="row.tsCount" :disabled="disableForm" />
        </template>
        <template v-slot:tsAmount="{row}">
          <el-input v-model="row.tsAmount" :disabled="disableForm" />
        </template>
        <template v-slot:asCount="{row}">
          <el-input v-model="row.asCount" :disabled="disableForm" />
        </template>
        <template v-slot:asAmount="{row}">
          <el-input v-model="row.asAmount" :disabled="disableForm" />
        </template>
        <template v-slot:ctsCount="{row}">
          <el-input v-model="row.ctsCount" :disabled="disableForm" />
        </template>
        <template v-slot:ctsAmount="{row}">
          <el-input v-model="row.ctsAmount" :disabled="disableForm" />
        </template>
        <template v-slot:casCount="{row}">
          <el-input v-model="row.casCount" :disabled="disableForm" />
        </template>
        <template v-slot:casAmount="{row}">
          <el-input v-model="row.casAmount" :disabled="disableForm" />
        </template>
        <template v-slot:remark="{row}">
          <el-input v-model="row.remark" :disabled="disableForm" />
        </template>
      </com-table>
      <el-button style="margin-top:10px;" type="primary" @click="showFileModal = true">附件资料({{ fileList.length }})</el-button>
      <file-uploader v-model="fileList" :show-modal.sync="showFileModal" :disabled="disableForm" :can-download="optType==='detail'" />

    </el-form>
    <el-dialog
      title="新增清单结算"
      :visible.sync="showAddDetailModal"
      width="700px"
      center
      append-to-body
    >
      <el-form ref="detailForm" :rules="detailRules" style="padding-top: 10px;" :model="detailDataItem" label-width="110px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="合同-清单编号" prop="conNumber">
              <el-input v-model="detailDataItem.conNumber" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="合同清单名称" prop="conName">
              <el-input v-model="detailDataItem.conName" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="合同-单位" prop="conUnit">
              <el-input v-model="detailDataItem.conUnit" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="合同-单价(元)" prop="conUnitPrice">
              <el-input v-model="detailDataItem.conUnitPrice" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="合同-合价(元)" prop="conSumPrice">
              <el-input v-model="detailDataItem.conSumPrice" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="合同-数量" prop="conCount">
              <el-input v-model="detailDataItem.conCount" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="本次结算-结算数量" prop="tsCount">
              <el-input v-model="detailDataItem.tsCount" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="本次结算-结算金额" prop="tsAmount">
              <el-input v-model="detailDataItem.tsAmount" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="开累结算-结算数量" prop="asCount">
              <el-input v-model="detailDataItem.asCount" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="开累结算-结算金额" prop="asAmount">
              <el-input v-model="detailDataItem.asAmount" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="按合同本次结算-结算数量" prop="ctsCount">
              <el-input v-model="detailDataItem.ctsCount" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="按合同本次结算-结算金额" prop="ctsAmount">
              <el-input v-model="detailDataItem.ctsAmount" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="按合同开累结算-结算数量" prop="casCount">
              <el-input v-model="detailDataItem.casCount" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="按合同开累结算-结算金额" prop="casAmount">
              <el-input v-model="detailDataItem.casAmount" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="detailDataItem.remark" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelAddDetail">取 消</el-button>
        <el-button type="success" icon="el-icon-plus" @click="addDetail">确 定</el-button>
      </span>
    </el-dialog>
    <transfer-list :show-modal.sync="showTransferListModal" @getSelectedRows="getSelectedRowsTransfer" />
  </div>
</template>

<script>
import { addTransferContractSettleInfo, updateTransferContractSettleInfo, getTransferContractSettleInfoDetail } from '@/api/contractManage'
import { getAreaList } from '@/api/common'
import { workflowStatus } from '@/utils/dicData'
import fileUploader from '@/components/FileUploader'
// import ProjectList from '@/components/TreeModal/project'
import TransferList from '@/components/TreeModal/transfer'
export default {
  name: 'AddTransferContractSettleInfo',
  components: {
    fileUploader,
    TransferList
  },
  props: {
    showModal: {
      type: Boolean,
      default: false
    },
    optType: {
      type: String,
      default: 'add'
    },
    dir: {
      type: Object,
      default() {
        return {}
      }
    },
    data: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      showFileModal: false,
      targetList: [],
      workflowStatus,

      addType: '', // same   under
      visible: false,
      showAdd: false,
      selectedNode: null,
      form: {
        settleNumber: '',
        teamNumber: '',
        teamName: '',
        contractNumber: '',
        contractName: '',
        contractAmount: '',
        period: '',
        thisSettleAmount: '',
        accruedSettleAmount: '',
        projectName: '',
        addWorkflowId: '',
        status: '',
        remark: '',
        founderName: '',
        founderUnit: '',
        createTime: ''
      },
      showAddDetailModal: false,
      selectRow: null,
      detailDataList: [],
      detailDataItem: {
      },
      columns: [
        {
          label: '合同-清单编号',
          slot: 'conNumber'
        },
        {
          label: '合同清单名称',
          slot: 'conName'
        },
        {
          label: '合同-单位',
          slot: 'conUnit'
        },
        {
          label: '合同-单价(元)',
          slot: 'conUnitPrice'
        },
        {
          label: '合同-合价(元)',
          slot: 'conSumPrice'
        },
        {
          label: '合同-数量',
          slot: 'conCount'
        },
        {
          label: '本次结算-结算数量',
          slot: 'tsCount'
        },
        {
          label: '本次结算-结算金额',
          slot: 'tsAmount'
        },
        {
          label: '开累结算-结算数量',
          slot: 'asCount'
        },
        {
          label: '开累结算-结算金额',
          slot: 'asAmount'
        },
        {
          label: '按合同约定本期结算-结算数量',
          slot: 'ctsCount'
        },
        {
          label: '按合同约定本期结算-结算金额',
          slot: 'ctsAmount'
        },
        {
          label: '按合同约定开累结算-结算数量',
          slot: 'casCount'
        },
        {
          label: '按合同约定开累结算-结算金额',
          slot: 'casAmount'
        },
        {
          label: '备注',
          slot: 'remark'
        }
      ],
      showTargetSetListModal: false,
      showTransferListModal: false,
      fileList: [
      ],
      rules: {
        unifiedSocialCreditCode: [
          { required: true, message: '统一社会信用代码', trigger: 'change' }
        ]
      },
      detailRules: {
        conNumber: [
          { required: true, message: '请输入合同-清单编号', trigger: 'change' }
        ],
        conName: [
          { required: true, message: '请输入合同清单名称', trigger: 'change' }
        ],
        conUnit: [
          { required: true, message: '请输入合同-单位', trigger: 'change' }
        ],
        conUnitPrice: [
          { required: true, message: '请输入合同-单价(元)', trigger: 'change' }
        ],
        conSumPrice: [
          { required: true, message: '请输入合同-合价(元)', trigger: 'change' }
        ],
        conCount: [
          { required: true, message: '请输入合同-数量', trigger: 'change' }
        ],
        tsCount: [
          { required: true, message: '请输入本次结算-结算数量', trigger: 'change' }
        ],
        tsAmount: [
          { required: true, message: '请输入本次结算-结算金额', trigger: 'change' }
        ],
        asCount: [
          { required: true, message: '请输入开累结算-结算数量', trigger: 'change' }
        ],
        asAmount: [
          { required: true, message: '请输入开累结算-结算金额', trigger: 'change' }
        ],
        ctsCount: [
          { required: true, message: '请输入按合同本次结算-结算数量', trigger: 'change' }
        ],
        ctsAmount: [
          { required: true, message: '请输入按合同本次结算-结算金额', trigger: 'change' }
        ],
        casCount: [
          { required: true, message: '请输入按合同开累结算-结算数量', trigger: 'change' }
        ],
        casAmount: [
          { required: true, message: '请输入按合同开累结算-结算金额', trigger: 'change' }
        ],
        remark: [
          { required: true, message: '请输入备注', trigger: 'change' }
        ]
      },
      provinceData: [],
      cityData: []
    }
  },
  computed: {
    disableForm() {
      return this.optType === 'detail'
    },
    show: {
      get() {
        return this.showModal
      },
      set(val) {
        this.$emit('update:showModal', val)
        this.fileList = []
      }
    }
  },
  watch: {
    data: {
      handler(newData, oldData) {
        if (newData.businessId) {
          this.getDocDetail(newData.businessId)
        }
      },
      immediate: true
    }
  },
  mounted() {
    this.getRegionData()
  },
  methods: {
    getSelectedRowsTransfer(obj) {
      this.form.contractNumber = obj.contractNumber
      this.form.contractName = obj.contractName
      this.form.teamNumber = obj.teamNumber
      this.form.teamName = obj.teamName
      this.form.projectName = obj.projectName
      this.form.contractAmount = obj.initAmount
    },
    addDetail() {
      this.$refs.detailForm.validate((valid) => {
        if (valid) {
          const item = JSON.parse(JSON.stringify(this.detailDataItem))
          this.detailDataList.push(item)
          this.showAddDetailModal = false
          this.$refs.detailForm.resetFields()
        }
      })
    },
    cancelAddDetail() {
      this.showAddDetailModal = false
    },
    delDetailDataItem() {
      if (this.selectRow) {
        const index = this.detailDataList.indexOf(this.selectRow)
        this.detailDataList.splice(index, 1)
      }
    },
    provinceChange(e) {
      const selectedData = this.provinceData.filter(val => val.name === e)[0]
      if (selectedData) {
        this.form.city = ''
        const params = {
          parentCode: selectedData.code,
          areaType: 2
        }
        getAreaList(params).then(res => {
          this.cityData = res.data || []
        })
      }
    },
    getDocDetail(id) {
      getTransferContractSettleInfoDetail({ transferContractSettleInfoId: id }).then(res => {
        const { lstAttachment, lstTransferContractSettleChecklist, transferContractSettleInfo } = res.data || {}
        Object.keys(this.form).forEach(key => {
          const source = transferContractSettleInfo[key]
          if (typeof source === 'boolean') {
            this.form[key] = source ? 1 : 0
          } else {
            this.form[key] = source
          }
        })
        if (lstAttachment && lstAttachment.length > 0) {
          this.fileList = lstAttachment
        }
        if (lstTransferContractSettleChecklist && lstTransferContractSettleChecklist.length > 0) {
          this.detailDataList = lstTransferContractSettleChecklist
        }
      })
    },
    getRegionData() {
      getAreaList({ areaType: '1' }).then(res => {
        this.provinceData = res.data || []
      })
    },
    deleteDetail() {
    },
    saveDoc() {
      if (this.optType === 'detai') {
        this.show = false
        return
      }
      this.$refs.form.validate((valid) => {
        if (valid) {
          const params = {
            transferContractSettleInfo: {
              ...this.form,
              ...this.optType === 'edit' ? {
                id: this.data.id
              } : {}
            },
            lstTransferContractSettleChecklist: this.detailDataList.map(item => {
              return {
                ...item,
                ...this.optType === 'edit' ? {
                  transferContractSettleInfoId: this.data.id
                } : {}
              }
            }),
            lstAttachment: this.fileList
          }
          const fn = this.optType === 'add' ? addTransferContractSettleInfo : updateTransferContractSettleInfo
          fn(params).then(res => {
            if (res.code === '0001') {
              this.$message({
                type: 'success',
                message: '保存成功!'
              })
              this.show = false
              this.$emit('freshList')
              this.$refs.form.resetFields()
            } else {
              this.$message({
                type: 'error',
                message: res.msg
              })
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.com-form-title{
  font-size: 14px;
  font-weight: bold;
  line-height: 35px;
  color: #409EFF;
  border-bottom: 1px solid #dedede;
  margin-bottom: 6px;
}
::v-deep .el-form-item__label{
  color: #000;
}
::v-deep .el-input.is-disabled .el-input__inner{
  color: #000;
}
::v-deep .el-input.is-disabled .el-input__inner{
  color: #000;

}
</style>
